﻿using UnityEngine;
using System.Collections;

public class Line : GameUI
{

    float m_fCurrentTime = 0.0f;
    
    public GameObject m_pOwner = null;
    private Vector3 m_vecDestPos;
    private Vector3 m_vecDestNormal; 
    private float m_fStartLen = 0.0f;

    private float m_fLineLength;

    private Vector3 m_vecStartPos;

   public void SetLine(Vector3 vecStartPos, Vector3 vecDestPos)
   {
       m_vecStartPos = vecStartPos;
       m_vecDestPos = vecDestPos;

       Vector3 vecNormal = vecStartPos - vecDestPos;
       float vecLength = Vector2.Distance(vecStartPos, vecDestPos);

       vecNormal = Vector3.Normalize(vecNormal);
       m_vecDestNormal = vecNormal;

       vecNormal = (vecNormal * (vecLength / 2));

       Calculate.RotToDest(gameObject, vecDestPos);

       transform.position -= vecNormal; 
       transform.localScale = new Vector3(transform.localScale.x, 1.0f * (vecLength * 100), transform.localScale.z);

       m_fLineLength = vecLength;
       m_fStartLen = (vecLength * 100);
   }

   public void Refresh_OwnerPos(Vector3 vecStartPos)
   {
       m_vecStartPos = vecStartPos;

       Vector3 vecNormal = vecStartPos - m_vecDestPos;
       vecNormal = Vector3.Normalize(vecNormal);
       m_vecDestNormal = vecNormal;
   }

   // Update is called once per frame
   void Update()
   {
       m_fCurrentTime += Time.deltaTime;

       if (m_fCurrentTime > 0.5f)
       {
           if (gameObject.GetComponent<SpriteRenderer>().color.a >= 0.5f)
               ChangeAlpha(1 * Time.deltaTime);
       }

       float vecLength = Vector2.Distance(m_pOwner.transform.position, m_vecDestPos);
       if (vecLength < 0.01f)
           Destroy(gameObject);

       transform.position = m_pOwner.transform.position - (m_vecDestNormal * (vecLength / 2));

       m_fLineLength = vecLength;

       transform.localScale = new Vector3(transform.localScale.x, 1.0f * (vecLength * 100.0f), transform.localScale.z);



   }
}
